using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using TreeGenerator;
using System.Xml;

namespace TestTreeGenerator
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnShowTree_Click(object sender, EventArgs e)
        {

        }
       // private TreeGenerator.
        private TreeBuilder myTree=null;
        private void ShowTree()
        {
            picTree.Image = Image.FromStream(myTree.GenerateTree(800, -1, "1000", System.Drawing.Imaging.ImageFormat.Bmp));
        }
        private void SetControlValues()
        {
            if (myTree != null)
            {
                //lblBGColor.BackColor = myTree.BGColor;
               // lblBoxFillColor.BackColor = myTree.BoxFillColor;
               // lblFontColor.BackColor = myTree.FontColor;
               // lblLineColor.BackColor = myTree.LineColor;
                nudBoxHeight.Value = Convert.ToDecimal( myTree.BoxHeight);
                nudBoxWidth.Value  = Convert.ToDecimal( myTree.BoxWidth);
                nudFontSize.Value = Convert.ToDecimal( myTree.FontSize);
                nudHorizontalSpace.Value = Convert.ToDecimal( myTree.HorizontalSpace);
                nudLineWidth.Value =Convert.ToDecimal(  myTree.LineWidth);
                nudMargin.Value =Convert.ToDecimal(  myTree.Margin);
                nudVerticalSpace.Value = Convert.ToDecimal(myTree.VerticalSpace);
            
            
            }
        
        }

        private void lblFontColor_Click(object sender, EventArgs e)
        {
            DialogResult result;

            result = colorDialog1.ShowDialog();

            if (result == DialogResult.Cancel)
                return;

            myTree.FontColor = colorDialog1.Color;
            ShowTree();
        }

        private void lblBoxFillColor_Click(object sender, EventArgs e)
        {
            DialogResult result;

            result = colorDialog1.ShowDialog();

            if (result == DialogResult.Cancel)
                return;

            myTree.BoxFillColor  = colorDialog1.Color;
            ShowTree();
        }

        private void lblLineColor_Click(object sender, EventArgs e)
        {
            DialogResult result;

            result = colorDialog1.ShowDialog();

            if (result == DialogResult.Cancel)
                return;

            myTree.LineColor = colorDialog1.Color;
            ShowTree();
        }

        private void lblBGColor_Click(object sender, EventArgs e)
        {
            DialogResult result;

            result = colorDialog1.ShowDialog();

            if (result == DialogResult.Cancel)
                return;

            myTree.BGColor = colorDialog1.Color;
            ShowTree();
        }

        private void nudLineWidth_ValueChanged(object sender, EventArgs e)
        {
            myTree.LineWidth =(float) nudLineWidth.Value;
            ShowTree();
        }

        private void nudFontSize_ValueChanged(object sender, EventArgs e)
        {
            myTree.FontSize = (int)nudFontSize.Value;
            ShowTree();
        }

        private void nudVerticalSpace_ValueChanged(object sender, EventArgs e)
        {
            myTree.VerticalSpace = (int)nudVerticalSpace.Value;
            ShowTree();
        }

        private void nudHorizontalSpace_ValueChanged(object sender, EventArgs e)
        {
            myTree.HorizontalSpace = (int)nudHorizontalSpace.Value;
            ShowTree();
        }

        private void nudMargin_ValueChanged(object sender, EventArgs e)
        {
            myTree.Margin = (int)nudMargin.Value;
            ShowTree();
        }

        private void nudBoxHeight_ValueChanged(object sender, EventArgs e)
        {
            myTree.BoxHeight = (int)nudBoxHeight.Value;
            ShowTree();
        }

        private void nudBoxWidth_ValueChanged(object sender, EventArgs e)
        {
            myTree.BoxWidth = (int)nudBoxWidth.Value;
            ShowTree();
        }

        private void picOrgChart_MouseClick(object sender, MouseEventArgs e)
        {
            Rectangle currentRect;
            //determine if the mouse clicked on a box, if so, show the  node description.
            string SelectedNode = "No Node";
            //find the node
            foreach (XmlNode oNode in myTree.xmlTree.SelectNodes("//Node"))
            { 
                //iterate through all nodes until found.
                currentRect = myTree.getRectangleFromNode(oNode);
                if (e.X >= currentRect.Left &&
                    e.X <= currentRect.Right &&
                    e.Y >= currentRect.Top &&
                    e.Y <= currentRect.Bottom)
                {
                    SelectedNode = oNode.Attributes["nodeDescription"].InnerText;
                    break;
                }
                
               
            }
            
            MessageBox.Show(SelectedNode);
        }

        private void btnShowChart_Click(object sender, EventArgs e)
        {
            ShowTree();

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            TreeGenerator.TreeData.TreeDataTableDataTable dtTree = new TreeData.TreeDataTableDataTable();
            dtTree.AddTreeDataTableRow("1000", "", "1000", "");
            dtTree.AddTreeDataTableRow("2000", "1000", "2000", "");
            dtTree.AddTreeDataTableRow("2001", "1000", "2001", "");
            dtTree.AddTreeDataTableRow("3000", "2000", "3000", "");
            dtTree.AddTreeDataTableRow("3001", "2000", "3001", "");
            dtTree.AddTreeDataTableRow("3000", "2001", "3000", "");
            dtTree.AddTreeDataTableRow("4000", "3000", "4000", "");
            dtTree.AddTreeDataTableRow("4001", "3000", "4001", "");
            //instantiate the object
            //myOrgChart = new OrgChartGenerator.OrgChart(myOrgData);
            myTree = new TreeBuilder(dtTree);
            SetControlValues();    
        }

        

       
    }
    }
